package top.forforever.srb.sms.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import top.forforever.srb.mq.pojo.dto.SmsDTO;
import top.forforever.srb.mq.util.MQConst;
import top.forforever.srb.sms.service.ApiSmsService;

import java.io.IOException;


/**
 * @create: 2023/6/9
 * @Description:
 * @FileName: SmsMqReceiver
 * @自定义内容： 消费方
 */
@Component
public class SmsMqReceiver {

    @Autowired
    private ApiSmsService apiSmsService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MQConst.EXCHANGE_TOPIC_SMS), //交换机
            key = {MQConst.ROUTING_SMS_ITEM}, // 路由
            value = @Queue(value = MQConst.QUEUE_SMS_ITEM, durable = "true", autoDelete = "false") //队列
    ))
    public void rechargeSms(Channel channel, Message message, SmsDTO smsDTO) {
        byte[] body = message.getBody();
        String mes = new String(body);
        System.out.println("消费端");

        //发送短信消息
        apiSmsService.sendRechargeSuccess(smsDTO);

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
